package com.quasar.hpatient.view.personal.register.registerFragment;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.BloodTypeInfodb;
import com.quasar.hpatient.model.litePalmodel.KidneySourceTypedb;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.onInterface.viewInterface.UiInterface;
import com.quasar.hpatient.presenter.AloneUserPresenter;
import com.quasar.hpatient.view.personal.register.RegisterEditActivity;
import com.quasar.hpatient.widget.SelectWidget;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/11/27 0027
 * View: 器官来源
 */

@EFragment(R.layout.register_organ_fragment_layout)
public class RegisterOrganFragment extends BaseFragment implements UiInterface<DataStateResult> {

    private AloneUserPresenter aloneUserPresenter;
    List<KidneySourceTypedb> lidney = MainAppliaction.getInstance().getKidneySourceTypeList();
    //记录器官来源id
    int sourceId = 0;

    @ViewById(R.id.sw_source)
    SelectWidget sw_source;
    @FragmentArg
    PatientInfoData patient;
    List<KidneySourceTypedb> kidneySourceTypedbList = MainAppliaction.getInstance().getKidneySourceTypeList();


    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Click(R.id.tv_next)
    public void tv_next() {
        //器官来源id
        if (sourceId == 0) {
            msg("请选择器官来源");
            return;
        }
        aloneUserPresenter.AloneUserData(ConstantManger.USERSOURCEID, sourceId + "", ConstantManger.TABLEOPERATION);
    }

    @Click(R.id.tv_last)
    public void tv_last() {
        RegisterEditActivity.ViewPage(4);
    }

    @Click(R.id.tv_skip)
    public void tv_skip() {
        RegisterEditActivity.ViewPage(6);
    }

    @Click(R.id.sw_source)
    public void sw_source() {
        //选择器官来源
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = lidney.get(options1).getPickerViewText();
                sw_source.setText(tx);
                sourceId = lidney.get(options1).getId();
            }
        })

                .setTitleText("器官来源选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .setOutSideCancelable(false)// default is true
                .build();
        pvOptions.setPicker(lidney);//三级选择器
        pvOptions.show();
    }

    @Override
    public void initView() {

        if (patient.getPatientOperationInfo() != null) {
            if (patient.getPatientOperationInfo().getSourceId() != 0) {
                for (int i = 0; i < kidneySourceTypedbList.size(); i++) {
                    if (kidneySourceTypedbList.get(i).getPrimaryid() == patient.getPatientOperationInfo().getSourceId()) {
                        sw_source.setText(kidneySourceTypedbList.get(i).getName());
                        sourceId=patient.getPatientOperationInfo().getSourceId();
                        break;
                    }
                }

            }
        }
    }

    @Override
    public void initData() {
        aloneUserPresenter = new AloneUserPresenter(this);
    }

    @Override
    public void onViewSuccess(DataStateResult data) {
        if (data.getMsgCode().equals("0x00000000")) {
            RegisterEditActivity.ViewPage(6);
        }
    }

    @Override
    public void onViewError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onViewRefreshUI() {

    }
}
